# -*- coding: utf-8 -*-
import sys
sys.path.append("../../quidditas")

from random import randint

import pygame
import quidditas

from components import *

class MovementProcessor(quidditas.Processor):
    """ Update entities that are moving through the world and
    make sure they don't leave the world. """
    component_types = (PositionComponent, VelocityComponent)

    def __init__(self, size):
        """ Create a MovementProcessor for the given world size. """
        super(MovementProcessor, self).__init__()
        self.world_size = size

    def update_entity(self, time, entity, components):
        width = self.world_size[0]
        height = self.world_size[1]
        comp = components[PositionComponent]
        velocity = components[VelocityComponent]
        
        comp.x += velocity.x * time / 1000.0
        if comp.x < 0:
            comp.x = width
        elif comp.x > width:
            comp.x = 0

        comp.y += velocity.y * time / 1000.0
        if comp.y < 0:
            comp.y = height
        elif comp.y > height:
            comp.y = 0

class ControlsProcessor(quidditas.Processor):
    component_types = ()

    def __init__(self, event_bus):
        super(ControlsProcessor, self).__init__()
        self.event_bus = event_bus

    def update(self, time):
        for event in pygame.event.get((pygame.KEYDOWN)):
            if event.key == pygame.K_ESCAPE:
                print "exiting!"
                print self.event_bus.has_any_subscriptions("event.exit")
                self.event_bus.publish("event.exit")

class AsteroidSpawner(quidditas.Processor):
    """ Spawns asteroids in regular intervals. """
    component_types = ()

    def __init__(self):
        super(AsteroidSpawner, self).__init__()
        self.current_asteroids = 0
        self.interval = 500
        self.current_timer = 0

    def update(self, time):
        if self.current_asteroids >= 20:
            return

        if self.current_timer > 0:
            self.current_timer -= time
        else:
            self.current_timer = self.interval
            self.current_asteroids += 1
            e = self.quidditas.create_entity("asteroid")
            pos = e.get_component(PositionComponent)
            pos.x = randint(0, 640)
            pos.y = randint(0, 480)
            pos.rotation = randint(0, 360)
            vel = e.get_component(VelocityComponent)
            vel.x = randint(-50, 50)
            vel.y = randint(-50, 50)

class RenderProcessor(quidditas.Processor):
    """ Renders all visible entities. """
    component_types = (PositionComponent, RenderingComponent)
    
    def __init__(self, screen, images):
        """ Create a new renderprocessor that renders to screen and
        uses images as the image source. """
        super(RenderProcessor, self).__init__()
        self.screen = screen
        self.images = images
        self.super_update = super(RenderProcessor, self).update

    def update(self, time):
        """ This method is overridden to clear the screen on each draw-update before drawing the components. """
        self.screen.fill((0,0,0))
        self.super_update(time)

    def update_entity(self, time, entity, components):
        pos = components[PositionComponent]
        render = components[RenderingComponent]
        rotated = pygame.transform.rotate(self.images[render.image], pos.rotation)
        self.screen.blit(rotated, (pos.x, pos.y))
